package com.mcarthur.countdown.client.drawable;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.client.Window;
import com.googlecode.gwtgl.array.Float32Array;
import com.googlecode.gwtgl.binding.WebGLBuffer;
import com.googlecode.gwtgl.binding.WebGLRenderingContext;
import com.mcarthur.countdown.client.renderer.CountdownRenderer;

public class Ball extends AbstractDrawable {
	public static final int WIDTH = 14;
	public static final int RADIUS = 7;	
	public static int colorLoc;
	public static int transVectorLoc = -1;

	public static final Color INACTIVE = new Color(221.f/255.f, 221.f/255.f, 221.f/255.f);

    private boolean active = false;
	protected Color activeColor;
	private WebGLBuffer transBuffer;
	private static Logger logger = Logger.getLogger("");

	public Ball(float posX, Color activeColor) {
		super(posX);
		setActiveColor(activeColor);
		transBuffer = CountdownRenderer.gl.createBuffer();
    }

    public void draw(WebGLRenderingContext gl) {
       
       gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, transBuffer);
       
       // Works with 3 elements instead of 2
       gl.vertexAttribPointer(transVectorLoc, 3, WebGLRenderingContext.FLOAT, false, 0, 0);

//       gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, CountdownRenderer.vertexTextureCoordBuffer);
//       gl.vertexAttribPointer(CountdownRenderer.textureCoordAttribute, 2, WebGLRenderingContext.FLOAT, false, 0, 0);
//       
//       gl.activeTexture(WebGLRenderingContext.TEXTURE0);
//       gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, CountdownRenderer.texture);
//
//       gl.uniform1i(CountdownRenderer.textureUniform, 0);
       gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4);
    }
    
    public void setActive(boolean active) {
		this.active = active;
	}
    
    public boolean isActive() {
    	return active;
    }
    
    public void setActiveColor(Color activeColor) {
    	this.activeColor = activeColor;
    }
    
    public Color getActiveColor() {
		return activeColor;
	}
    
    @Override
    public void setPosY(float posY) {
        super.setPosY(posY);
        
        //TODO: might be bad in the bouncing case
        
        CountdownRenderer.gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, transBuffer);

        // Adding z value fixed it ...
        float[] trans = new float[] {posX, this.posY, 0.f,
                                     posX, this.posY, 0.f,
                                     posX, this.posY, 0.f,
                                     posX, this.posY, 0.f};
        
        CountdownRenderer.gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, Float32Array.create(trans),
                WebGLRenderingContext.STATIC_DRAW);
        
        //CountdownRenderer.gl.vertexAttribPointer(transVectorLoc, 3, WebGLRenderingContext.FLOAT, false, 0, 0);
        
        // This does the same thing as the attrib pointer above ...
        //CountdownRenderer.gl.vertexAttrib2f(transVectorLoc, posX, posY);
    }
}
